home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / hist.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  160 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    hist - 
  19.  *        Support for histogram creation and display.
  20.  *
  21.  *                Paul Haeberli - 1988
  22.  */
  23. #include "hist.h"
  24. #include "gl.h"
  25.  
  26. histogram *newhist(min,max,nbuckets)
  27. int min, max;
  28. int nbuckets;
  29. {
  30.     histogram *hist;
  31.     int i, *ptr;
  32.  
  33.     hist = (histogram *)mymalloc(sizeof(histogram));
  34.     hist->nbuckets = nbuckets;
  35.     hist->bucket = (int *)mymalloc(nbuckets*sizeof(int));
  36.     hist->min = min;
  37.     hist->max = max;
  38.     clearhist(hist);
  39.     return hist;
  40. }
  41.  
  42. freehist(hist)
  43. histogram *hist;
  44. {
  45.     if(hist) {
  46.         free(hist->bucket);
  47.         free(hist);
  48.     }
  49. }
  50.  
  51. clearhist(hist)
  52. histogram *hist;
  53. {
  54.     int i, nbuckets, *ptr;
  55.  
  56.     ptr = hist->bucket;
  57.     nbuckets = hist->nbuckets;
  58.     for (i=0; i<nbuckets; i++)
  59.     *ptr++ = 0;
  60. }
  61.  
  62. addtohist(hist,sptr,n)
  63. histogram *hist;
  64. unsigned short *sptr;
  65. int n;
  66. {
  67.     unsigned int index, nbuckets;
  68.     int *bucket;
  69.  
  70.     nbuckets = hist->nbuckets;
  71.     bucket = hist->bucket;
  72.     while (n--) {
  73.     index = *sptr++;
  74.     if(index>=nbuckets) 
  75.         index = nbuckets-1;
  76.     bucket[index]++;
  77.     }
  78. }
  79.  
  80. showhist(hist)
  81. histogram *hist;
  82. {
  83.     int nbuckets;
  84.     int i, bmax, j, total, *ptr;
  85.  
  86.     nbuckets = hist->nbuckets;
  87.     total = bmax = 0;
  88.     ptr = hist->bucket;
  89.     for (i=0; i<nbuckets; i++) {
  90.     total += *ptr;
  91.     if(*ptr>bmax)
  92.         bmax = *ptr;
  93.     ptr++;
  94.     }
  95.     if(bmax==0) 
  96.     total = bmax = 1;
  97.  
  98.     ortho2(0.0,(float)nbuckets,0.0,(float)bmax);
  99.     grey(0.4);
  100.     clear();
  101.  
  102.     grey(0.5);
  103.     ptr = hist->bucket;
  104.     for (i=0; i<nbuckets; i++) 
  105.     rectf(i+0.1,0.0,i+0.9,5.0*(float)(*ptr++));
  106.  
  107.     for (i=0; i<nbuckets; i+=10)  {
  108.     if((i%100) == 0)
  109.         grey(0.0);
  110.     else
  111.         grey(0.2);
  112.     move2i(i,0);
  113.     draw2i(i,bmax);
  114.     }
  115.  
  116.     grey(0.8);
  117.     ptr = hist->bucket;
  118.     for (i=0; i<nbuckets; i++) 
  119.     rectf(i+0.1,0.0,i+0.9,(float)(*ptr++));
  120.  
  121.     rgb(1.0,0.0,0.0);
  122.     move2i(hist->min,0);
  123.     draw2i(hist->min,bmax/40);
  124.     move2i(hist->max,0);
  125.     draw2i(hist->max,bmax/40);
  126.  
  127.     ortho2(0.0,(float)nbuckets,0.0,(float)total);
  128.     grey(0.0);
  129.     move2i(0,0);
  130.     j = 0;
  131.     ptr = hist->bucket;
  132.     for (i=0; i<nbuckets; i++) {
  133.     j += *ptr++;
  134.     draw2((float)i,(float)j);
  135.     }
  136. }
  137.  
  138. histeqtable(hist,tab)
  139. histogram *hist;
  140. short *tab;
  141. {
  142.     int i, sum, shade, nbuckets; 
  143.     int *bucket; 
  144.     float maxshade;
  145.  
  146.     nbuckets = hist->nbuckets;
  147.     bucket = hist->bucket;
  148.     sum = 0;
  149.     for(i=0; i<nbuckets; i++)
  150.     sum += bucket[i];
  151.     if(sum == 0) 
  152.     sum = 1;
  153.     maxshade = 255.0;
  154.     shade = 0;
  155.     for(i=0; i<nbuckets; i++) {
  156.     tab[i] = (shade*255.0)/sum;
  157.     shade += bucket[i];
  158.     }
  159. }
  160.